package com.central.apps.handler.project;

import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import com.central.apps.service.ManHourStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工时汇总业务逻辑
 *
 * @author lidachao
 * @date 2023/1/13
 */
@Component
@Slf4j
public class ManhourFillHandler extends BaseHandler {

    @Autowired
    private HandlerService handlerService;
    @Autowired
    private ManHourStatisticsService manHourStatisticsService;

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        if ("gq_manhourfilldtl".equals(objName)) {
            Integer taskid = MapUtils.getInteger(data,"taskid");
            String taskName = MapUtils.getString(data,"taskname");
            Integer proid = MapUtils.getInteger(data,"proid");
            String subjectno = MapUtils.getString(data,"subjectno");
            float manhour = MapUtils.getFloat(data,"manhour");

            List<Map<String, Object>> msList = handlerService.findBySql("select * from gq_manhourstatistics where proid = " + proid + " and taskid = " + taskid);

            if (CollectionUtils.isEmpty(msList)) {
                Map<String, Object> msMap = new HashMap<>();
                msMap.put("proid", proid);
                msMap.put("taskid", taskid);
                msMap.put("taskname", taskName);
                msMap.put("manhour", manhour);
                msMap.put("subjectno", subjectno);

                handlerService.insert("gq_manhourstatistics", msMap);
            } else {
                handlerService.updateBySql("update gq_manhourstatistics set manhour = " + manhour + " where proid = " + proid + " and taskid = " + taskid);
            }

            // 统计项目工时总计
            manHourStatisticsService.manHourStatistics(proid);
        }
    }
}
